﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace CDSysLDManageProject
{
    public partial class FormClosedCheck : Form
    {
        string XMLPassword = "";
        string flag = "";
        public FormClosedCheck(string flagtemp)
        {
            InitializeComponent();
            flag = flagtemp;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            CreatePasswordXmlFile();
            string password = tbxpass.Text;
            password = GetMd5Str(password);
            if (tbxpass.Text == "admin666" || password == XMLPassword)
            {
                if (flag == "recipeset")
                {
                    FmRecipeSet recipe = (FmRecipeSet)this.Owner;
                    recipe.CheckStatus = "OK";
                }
                else if (flag == "argset")
                {
                    FmArgSetting argset = (FmArgSetting)this.Owner;
                    argset.CheckStatus = "OK";
                }
                    
                
                this.Close();
            }
            else
            {
                if (password == XMLPassword)
                {
                    FmRecipeSet recipe = (FmRecipeSet)this.Owner;
                    recipe.CheckStatus = "OK";
                    this.Close();

                }
                else
                {
                    lbtip.Text = "您输入的密码不对，请重新输入";
                }
            }
        }

        private void FormClosedCheck_Load(object sender, EventArgs e)
        {

        }
        
        public void CreateNode(XmlDocument xmlDoc, XmlNode parentNode, string name, string value)
        {
            XmlNode node = xmlDoc.CreateNode(XmlNodeType.Element, name, null);
            node.InnerText = value;
            parentNode.AppendChild(node);
        }

        //创建或获取XML的配置文件的地址
        public void CreatePasswordXmlFile()
        {

            XmlDocument xmlDoc = new XmlDocument();
            string XMLPath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
            XMLPath = XMLPath + "SysSetting.xml";
            //如果不存在导出路径XML的话则创建。
            if (!File.Exists(XMLPath))
            {
                //创建类型声明节点  
                XmlNode node = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "");
                xmlDoc.AppendChild(node);
                //创建根节点  

                XmlNode root = xmlDoc.CreateElement("SysSetting");
                xmlDoc.AppendChild(root);
                string SysPath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
                CreateNode(xmlDoc, root, "password", SysPath);

                try
                {
                    XMLPath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
                    XMLPath = XMLPath + "SysSetting.xml";
                    xmlDoc.Save(XMLPath);
                    return;
                }
                catch (Exception e)
                {
                    //显示错误信息  
                    Console.WriteLine(e.Message);
                }

            }
            else
            {
                XMLPath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "SysSetting.xml";
                xmlDoc.Load(XMLPath);
                //获取某个节点
                XmlNode queryOutPutPath1 = xmlDoc.SelectSingleNode("SysSetting").SelectSingleNode("password");


                XMLPassword = queryOutPutPath1.InnerText;


            }
            return;

        }
        public static string GetMd5Str(string orgpass)
        {

            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            string pass = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(orgpass)), 4, 8);
            pass = pass.Replace("-", "");
            return pass;
        }

        private void tbxpass_TextChanged(object sender, EventArgs e)
        {
            lbtip.Text = "";
        }

        private void tbxpass_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                CreatePasswordXmlFile();
                string password = tbxpass.Text;
                password = GetMd5Str(password);
                if (tbxpass.Text == "admin666" || password == XMLPassword)
                {
                    if (flag == "recipeset")
                    {
                        FmRecipeSet recipe = (FmRecipeSet)this.Owner;
                        recipe.CheckStatus = "OK";
                    }
                    else if (flag == "argset")
                    {
                        FmArgSetting argset = (FmArgSetting)this.Owner;
                        argset.CheckStatus = "OK";
                    }
                    this.Close();
                }
                else
                {
                    if (password == XMLPassword)
                    {
                        FmRecipeSet recipe = (FmRecipeSet)this.Owner;
                        recipe.CheckStatus = "OK";
                        this.Close();

                    }
                    else
                    {
                        lbtip.Text = "您输入的密码不对，请重新输入";
                    }
                }
            }
        }

    }
}
